package com.dongdongshop.config;

import com.dongdongshop.myexception.MyException;
import com.dongdongshop.pojo.TbSeller;
import com.dongdongshop.service.SellerService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;

public class SellerLoginRealm extends AuthorizingRealm {

    @DubboReference
    private SellerService sellerService;

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {

        UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;

        TbSeller ts = sellerService.check(token.getUsername());

        if (ts == null) {

            return null;
        }

        if (!ts.getStatus().equals("1")) {

            throw new MyException("账号正在审核中,请耐心等待");
        }
        return new SimpleAuthenticationInfo(ts, ts.getPassword(), ByteSource.Util.bytes(ts.getSalt()), ts.getSellerId());
    }
}
